package com.wnn.demo.model.TaskApi.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wnn.demo.domain.Accessory;
import com.wnn.demo.domain.Event;
import com.wnn.demo.domain.Task;
import com.wnn.demo.domain.User;
import com.wnn.demo.model.TaskApi.mapper.EventMapper;
import com.wnn.demo.model.TaskApi.mapper.TaskApiMapper;
import com.wnn.demo.model.system.service.UserService;
import com.wnn.demo.model.task.mapper.AccessoryFileMapper;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

import static java.awt.SystemColor.info;

@Service
public class TaskApiService {

    @Autowired
    private TaskApiMapper taskApiMapper;

    @Autowired
    private AccessoryFileMapper accessoryFileMapper;

    @Autowired
    private UserService userService;

    @Autowired
    private EventMapper eventMapper;

    public PageInfo<Task> unfinishTask(Integer page, Integer limit) {
        PageHelper.startPage(page,limit);
        List<Task> info= taskApiMapper.unfinishTask();
        for (int i = 0; i < info.size(); i++) {
            List<Accessory> file = accessoryFileMapper.findFile(info.get(i).getId());
            for (int j = 0; j < file.size(); j++) {
                String filePath = file.get(j).getFilePath();
                String property = System.getProperty("user.dir");
                String filePath1 = property+"/src/main/resources/public/"+filePath;
                file.get(j).setFilePath(filePath1);
            }
            info.get(i).setAccessories(file);
        }
        return new PageInfo< >(info);
    }

    public Integer acceptTask(Task task){
        return taskApiMapper.acceptTask(task);
    }

    public PageInfo<Task> acceptedTask(Integer page, Integer limit) {
        PageHelper.startPage(page,limit);
        Subject subject = SecurityUtils.getSubject();
        String username = (String) subject.getPrincipal();
        User user = userService.findUserByName(username);
        List<Task> info = taskApiMapper.acceptedTask(user.getUserId());
        for (int i = 0; i < info.size(); i++) {
            List<Accessory> file = accessoryFileMapper.findFile(info.get(i).getId());
            for (int j = 0; j < file.size(); j++) {
                String filePath = file.get(j).getFilePath();
                String property = System.getProperty("user.dir");
                String filePath1 = property+"/src/main/resources/public/"+filePath;
                file.get(j).setFilePath(filePath1);
            }
            info.get(i).setAccessories(file);
        }
        return new PageInfo< >(info);

    }

    public PageInfo<Task> finishTask(Integer page, Integer limit) {
        PageHelper.startPage(page,limit);
        List<Task> info= taskApiMapper.finishTask();
        for (int i = 0; i < info.size(); i++) {
            List<Accessory> file = accessoryFileMapper.findFile(info.get(i).getId());
            for (int j = 0; j < file.size(); j++) {
                String filePath = file.get(j).getFilePath();
                String property = System.getProperty("user.dir");
                String filePath1 = property+"/src/main/resources/public/"+filePath;
                file.get(j).setFilePath(filePath1);
            }
            info.get(i).setAccessories(file);
        }
        return new PageInfo< >(info);
    }

    public Task checkTask(String id) {
        Task task = taskApiMapper.checkTask(id);
        List<Accessory> file = accessoryFileMapper.findFile(id);
        for (int j = 0; j < file.size(); j++) {
            String filePath = file.get(j).getFilePath();
            String property = System.getProperty("user.dir");
            String filePath1 = property+"/src/main/resources/public/"+filePath;
            file.get(j).setFilePath(filePath1);
        }
       task.setAccessories(file);
        return task;
    }

    public Integer save(Event event) {
       return eventMapper.save(event);
    }

    public Event findEvent(String id) {
        Event event = eventMapper.findEvent(id);
        String eventId = event.getId();
        List<Accessory> file = accessoryFileMapper.findFile(eventId);
        for (int j = 0; j < file.size(); j++) {
            String filePath = file.get(j).getFilePath();
            String property = System.getProperty("user.dir");
            String filePath1 = property+"/src/main/resources/public/"+filePath;
            file.get(j).setFilePath(filePath1);
        }
        event.setAccessories(file);
        return event;
    }
}
